<?

class weather {




// FUNCTION // fetch the weather reports from Yahoo
function fetch_reports($location_id, $cache){

	
	if(!empty($location_id)){
		
		
		if($cache){
			$memcache = new Memcache;
			foreach(unserialize(CHOSTS) as $cache_server){
				$memcache->addServer($cache_server, 11211);
			}
			//create an index key for memcache
			$key = md5('weather_yahoo_'.$location_id);
			//lookup value in memcache
			$result = $memcache->get($key);
		}
		
		if($result){
			return $result;
		}else{
		
			
			$doc = new DOMDocument();
			$doc->load("http://weather.yahooapis.com/forecastrss?p=".$location_id."&u=c");
		
		
			if($doc){
			
				if($conditiondts = $doc->getElementsByTagname('condition')){
				
					$weather_report = array();
					
					$weather_report['text'] = $conditiondts->item(0)->getAttribute('text');
					$weather_report['code'] = $conditiondts->item(0)->getAttribute('code');
					$weather_report['temp'] = $conditiondts->item(0)->getAttribute('temp');
					$weather_report['image_url'] = WEATHER_ICONS_DIR.$this->translate_weather_code($weather_report['code']).".gif";
			
					$weather_report['forecast'] = $a_forecast;
				
					$forecast = $doc->getElementsByTagname('forecast');
					$i = 0;
					
					foreach ($forecast as $forecast_node) {
					
						$a_forecast[$i]['day'] = $forecast_node->getAttribute('day');
						$a_forecast[$i]['date'] = $forecast_node->getAttribute('date');
						$a_forecast[$i]['date_stamp'] = strtotime($a_forecast[$i]['date']);
						$a_forecast[$i]['high'] = $forecast_node->getAttribute('high');
						$a_forecast[$i]['low'] = $forecast_node->getAttribute('low');
						$a_forecast[$i]['text'] = $forecast_node->getAttribute('text');
						$a_forecast[$i]['code'] = $forecast_node->getAttribute('code');
						$a_forecast[$i]['image_url'] = WEATHER_ICONS_DIR.$this->translate_weather_code($forecast_node->getAttribute('code')).".gif";
						
						$i++;
						
					}
					
					$weather_report['forecast'] = $a_forecast;
					
					// Set cache
					if($cache) $memcache->set($key,$weather_report,0,$cache['seconds']);
					
					return $weather_report;
			
				}else{
					return false;
				}
			
			}
	
		}
	
	}else{
		return false;
	}

}






// FUNCTION // convert weather code
function translate_weather_code($code) {


	$time = date('G'); // not local !!!
	$night = ($time <= 5 || $time >= 20); // night is between 8 pm and 6 am
	
	
		switch($code) {
			case 0: //tornado
			case 3: //severe thunderstorms
			case 4: //thunderstorms
			case 37: //isolated thunderstorms
			case 38: //scattered thunderstorms
			case 39: //scattered thunderstorms
				return "ThunderStorm"; 
			break;
			case 1: //tropical storm
			case 2: //hurricane
				return "WindyRain"; 
			break;
			case 5: //mixed rain and snow
			case 16: //snow
			case 41: //heavy snow
			case 42: //scattered snow showers
			case 43: //heavy snow
				return "Snow"; 
			break;	
			case 6: //mixed rain and sleet
			case 17: //hail
			case 18: //sleet
				return "Sleet";
			break;	
			case 7: //mixed snow and sleet
				return "IcyFrozenSnow";
			break;
			case 8: //freezing drizzle
				return "IcyDrizzle";
			break;
			case 9: //drizzle
				return "Drizzle";
			break;
			case 10: //freezing rain
			case 35: //mixed rain and hail
				return "IcyRain"; 
			break;
			case 11: //showers
			case 12: //showers
			case 40: //scattered showers
				return "Showers";
			break;
			case 13: //snow flurries
				return "LightSnow";
			break;
			case 14: // light snow showers
				return "MedSnow";
			break;
			case 15: // blowing snow
				return "WindySnow";
			break;
			case 19: // dust
				return "Dust";
			break;
			case 20: // foggy
				return "Fog";
			break;
			case 21: // haze
				return "Haze";
			break;
			case 22: // smoky
				return "Smoke";
			break;
			case 23: // blustery
			case 24: // windy
				return "Wind";
			break;
			case 25: // cold
				//return "Frigid";
			break;
			case 26: // cloudy
				return "Clouds";
			break;
			case 27: // mostly cloudy (night)
				return "MostlyCloudyNight";
			break;
			case 28: // mostly cloudy (day)
				return "MostlyCloudyDay";
			break;
			case 29: // partly cloudy (night)
				return "PartlyCloudyNight";
			break;
			case 30: // partly cloudy (day)
				return "PartlyCloudyDay";
			break;
			case 31: // clear (night)
				return "Moon";
			break;
			case 32: // sunny
				return "Sun";
			break;
			case 33: // fair (night)
				return "FairNight";
			break;
			case 34: // fair (day)
				return "FairDay";
			break;		
			case 36: // hot
				return "Hot";
			break;
			case 44: // partly cloudy
				if($night) return "PartlyCloudyNight";
				else return "PartlyCloudyDay";
			break;
			case 45: // thundershowers
			case 47: // isolated thundershowers
			     if($night) return "NightThunderStorm";
			     else return "SunnyThunderStorm";
			break;
			case 46: // snow showers
				if($night) return "NightSnow";
				else return "Snow";
			break;
			default:
				return "Unknown";
			break;
		}


}








}

?>